/**
 * VUEshop
 * ============================================================================
 * * 版权所有 2015-2027 深圳搜豹网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.vueshop.com.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: soubao-java 2020-07-22 $
 */<template>
  <div>
    <div>
      <div class="fixed-bar">
        <div class="item-title">
          <router-link
            class="back"
            :to="{name:'brands',params:{currentPage:$route.params.currentPage}}"
            title="返回列表"
          >
            <i class="fa fa-arrow-circle-o-left"></i>
          </router-link>
          <div class="subject">
            <h3>品牌管理</h3>
            <h5>品牌添加与修改</h5>
          </div>
        </div>
      </div>
      <el-form ref="form" :model="form" label-width="200px">
        <el-form-item label="品牌名称" required>
          <el-input v-model="form.name"></el-input>
          <p class="err-msg" v-if="errResult.name && !form.name">{{errResult.name}}</p>
        </el-form-item>
        <el-form-item label="品牌网址">
          <el-input v-model="form.url"></el-input>
        </el-form-item>
        <el-form-item label="上级分类">
          <el-cascader
            v-model="category"
            :options="goodsCategoryTree"
            :props="{label: 'name', value: 'id', checkStrictly: true}"
          ></el-cascader>
          <p class="notic">最多成为第三级,如果设置为第二级, 只选择第一级即可</p>
        </el-form-item>
        <el-form-item label="品牌logo">
          <el-upload
            class="avatar-uploader"
            :action="apiHead + '/mall/upload?type=brand'"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <el-image v-if="form.logo" :src="form.logo" style="width: 100px; height: 100px"></el-image>
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
          <p class="notic">请上传图片格式文件,建议尺寸大小: 150*50</p>
        </el-form-item>
        <el-form-item label="排序">
          <el-input v-model="form.sort"></el-input>
        </el-form-item>
        <el-form-item label="品牌描述" required>
          <el-input type="textarea" v-model="form.desc" style="width:350px"></el-input>
          <p class="err-msg" v-if="errResult.desc && !form.desc">{{errResult.desc}}</p>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit()" :loading="isLoading">确认提交</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import {
  getBrand,
  getGoodsCategoryTree,
  updateBrand,
  addBrand
} from "@/utils/api";
export default {
  data() {
    return {
      form: {
        desc: ""
      },
      goodsCategoryTree: [],
      category: [],
      errResult: {},
      isLoading: false
    };
  },
  created() {
    this.getGoodsCategoryTree();
    if (this.$route.params.id) {
      this.getBrand();
    }
  },
  methods: {
    getBrand() {
      var that = this;
      getBrand({
        id: this.$route.params.id
      }).then(function(res) {
        var category = [];
        category.push(res.cat_id1);
        if (res.cat_id2 != 0) {
          category.push(res.cat_id2);
        }
        that.form = res;
        that.category = category;
      });
    },
    getGoodsCategoryTree() {
      var that = this;
      getGoodsCategoryTree({ levels: "1,2" }).then(function(res) {
        that.goodsCategoryTree = res;
      });
    },
    handleAvatarSuccess(res) {
      this.form.logo = res.result;
      this.$forceUpdate();
    },
    beforeAvatarUpload(file) {
      const isPic =
        file.type === "image/gif" ||
        file.type === "image/jpg" ||
        file.type === "image/jpeg" ||
        file.type === "image/png";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isPic) {
        this.$message.error("上传图片只能是 JPG、JPEG、PNG、GIF 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传图片大小不能超过 2MB!");
      }
      return isPic && isLt2M;
    },
    onSubmit() {
      this.isLoading = true;
      var that = this;
      this.form.cat_id1 = this.category[0];
      this.form.cat_id2 = this.category[1] ? this.category[1] : 0;
      if (this.form.id) {
        //编辑
        updateBrand(that.form).then(function(res) {
          if (res.status == 1) {
            that.$message.success({
              message: "保存成功",
              duration: 1000,
              onClose: function() {
                that.$router.push({ name: "brands" });
              }
            });
          } else if (res.status == -1) {
            that.$message.error("参数错误");
            that.errResult = res.result;
          } else {
            that.$message.error(res.msg);
          }
          that.isLoading = false;
        });
      } else {
        //添加
        addBrand(that.form).then(function(res) {
          if (res.status == 1) {
            that.$message.success({
              message: "添加成功",
              duration: 1000,
              onClose: function() {
                that.$router.push({ name: "brands" });
              }
            });
          } else if (res.status == -1) {
            that.$message.error("参数错误");
            that.errResult = res.result;
          } else {
            that.$message.error(res.msg);
          }
          that.isLoading = false;
        });
      }
    }
  }
};
</script>

<style scoped>
.avatar-uploader-icon {
  line-height: 100px;
}
</style>
